package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyDaoImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements CompanyService {


    @Override
    public ResultModel add(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel update(CompanyModel companyModel) {
        Jedis jedis = new Jedis();
        CompanyDao companyDao = new CompanyDaoImpl();
        int count = companyDao.update(companyModel);
        if (count > 0) {
            jedis.del("logo");
        }
        return ResultModel.getResultModel("", 1, count, null);
    }

    @Override
    public ResultModel delete(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel findAll(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel findById(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel getCount(CompanyModel companyModel) {
        return null;
    }

    @Override
    public ResultModel changeEnable(CompanyModel companyModel) {
        return null;
    }

    /**
     * 1.数据来源于redis
     * 2.判断redis里面是否存在？
     * 3.如果不存在，则需要查询数据库---将数据保存到redis
     * 4.如果存在，则将数据从redis里面取出来
     *
     * @return
     */
    @Override
    public CompanyModel findFrontCompanyModel() {
        final CompanyModel[] companyModel = {null};
        for (int i = 0; i < 500; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    companyModel[0] = getCompanyModel();
                    System.out.println(Thread.currentThread().getName() + "正在执行");
                }
            }, "线程" + i).start();
        }
        return companyModel[0];

    }

    private synchronized CompanyModel getCompanyModel() {
        //        1.创建redis   得到redis ,需要下载第三方jar包
        Jedis jedis = new Jedis("localhost", 6379);
//        2.判断redis里面是否存在公司信息
        Boolean key = jedis.exists("logo");
        if (key) {
            CompanyModel model = new CompanyModel();
            model.setLinePhone(jedis.get("linePhone"));
            model.setLogo(jedis.get("logo"));
            model.setPhone(jedis.get("phone"));
            model.setNet(jedis.get("net"));
            model.setEmail(jedis.get("email"));
            model.setAddress(jedis.get("address"));
            model.setVersion(jedis.get("version"));
            System.out.println("redis");
            return model;
        } else {
            CompanyDao companyDao = new CompanyDaoImpl();
//            思考：怎么讲数据库里面查询的数据保存到redis；
//            将model里面的数据存到redis里面的String里面去;
            CompanyModel model = companyDao.findFrontCompanyModel();
            jedis.set("logo", model.getLogo());
//            jedis.expire("logo", 10);
//            jedis.setex("logo", 1000, model.getLogo());
            jedis.set("phone", model.getPhone());
            jedis.set("email", model.getEmail());
            jedis.set("net", model.getNet());
            jedis.set("linePhone", model.getLinePhone());
            jedis.set("address", model.getAddress());
            jedis.set("version", model.getVersion());
            System.out.println("mysql");
            return model;
        }

    }
}
